package com.wuyoucar.controller;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TCarinfo;
import com.wuyoucar.service.InCarinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
    绑定车辆信息
    创建者:方铁鹏
 */
@RestController
@RequestMapping("/bindcar")
public class BindCarController {
    @Autowired
    InCarinfoService carinfoService;
    //绑定车辆
    @GetMapping("/carinfo/{vin}/{carnumber}/{cartype}/{firstchecktime}/{seatamount}/{userid}")
    public ResultBean insertCarinfo(@PathVariable("vin") String vin,
                                    @PathVariable("carnumber") String carnumber,
                                    @PathVariable("cartype") String cartype,
                                    @PathVariable("firstchecktime") String firstchecktime,
                                    @PathVariable("seatamount") int seatamount,
                                    @PathVariable("userid") int userid) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(firstchecktime);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        Date lastmodifytime = df.parse(time);
        TCarinfo tCarinfo = new TCarinfo(vin,carnumber,cartype,date,seatamount,lastmodifytime,userid);
        return carinfoService.insertCarinfo(tCarinfo);
    }

    //根据用户id 查询用户是否绑定车辆
    @GetMapping("/checkbindcar/{userid}")
    public ResultBean selectAllCarById(@PathVariable("userid")Integer userid){
        return carinfoService.selectAllCarById(userid);
    }
    //解除绑定
    @GetMapping("/unbunding/{vin}")
    public ResultBean unbunding(@PathVariable("vin")String vin){
        return carinfoService.unbinding(vin);
    }
}
